﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;
using SHRP.AppServices.Core;
using SHRP.UI.Command;
using System.IO;
using System.Resources;
using System.Globalization;
using System.Threading;
using SHRP.UI.Properties;


namespace SHRP.UI
{
    public partial class FrmMain : Form
    {
        static string configFile = Path.Combine(Path.GetDirectoryName(Application.ExecutablePath), "Layout.config");
        DeserializeDockContent deserializeDockContent;
        private TreePanel tree;
        private TestForm testform;
        private WelcomePanel welcome;

        public FrmMain()
        {
            InitializeComponent();
            CreateMDIForm();
            Application.Idle += new EventHandler(Application_Idle);
            CoreData.CoreDataContent[CoreDataType.ApplicationForm] = this;
            this.deserializeDockContent = new DeserializeDockContent(GetContentFormPersistString);
        }

        private IDockContent GetContentFormPersistString(string persistString)
        {
            if (persistString == typeof(TreePanel).ToString())
            {
                return this.tree;
            }
            else if (persistString == typeof(TestForm).ToString())
            {
                return this.testform;
            }
            else if (persistString == typeof(WelcomePanel).ToString())
            {
                return this.welcome;
            }
            return null;
        }

        private void OnActiveContextChanged(object sender, EventArgs e)
        {
            CoreData.CoreDataContent[CoreDataType.ActiveDockContent] = this.dockPanel1.ActiveContent;
        }
        
        void Application_Idle(object sender, EventArgs e)
        {
            Application.Idle -= new EventHandler(Application_Idle);
        }

        private void CreateMDIForm()
        {
            tree = new TreePanel();
            testform = new TestForm();
            welcome = new WelcomePanel();
        }

        private void LoadMDIForm()
        {
            CoreData.CoreDataContent[CoreDataType.ActiveDockPanel] = this.dockPanel1;
            tree.Show();
            welcome.Show();
            testform.Show();

        }

        protected override void OnClosing(CancelEventArgs e)
        {
            //base.OnClosing(e);
            if (!File.Exists(configFile))
            {
                this.dockPanel1.SaveAsXml(configFile);
            }
            CommandBase command = CommandManager.GetCommand(CommandFamily.Command_SystemClose);
            command.ExecuteAsyc(this, null);
            e.Cancel = true;
        }

        protected override void OnResize(EventArgs e)
        {
            if (this.WindowState == FormWindowState.Minimized)
            {
                this.Hide();
            }
            base.OnResize(e);
        }

        protected override void OnLoad(EventArgs e)
        {
            if (File.Exists(configFile))
            {
                try
                {
                    this.dockPanel1.LoadFromXml(configFile, deserializeDockContent);
                    CoreData.CoreDataContent[CoreDataType.ActiveDockPanel] = this.dockPanel1;
                }
                catch
                {
                    LoadMDIForm();
                }
            }
            else
            {
                LoadMDIForm();
            }
            base.OnLoad(e);
        }

        private void FrmMain_Load(object sender, EventArgs e)
        {

        }

        private void toolStripPrevous_Click(object sender, EventArgs e)
        {

        }

        private void toolStripNew_Click(object sender, EventArgs e)
        {
            DockContent content = CoreData.CoreDataContent[CoreDataType.ActiveDockContent] as DockContent;
        }
    }
}
